package cn.fql.pet.service.impl;

import cn.fql.basic.service.IBaiduAiAuditService;
import cn.fql.basic.util.AjaxResult;
import cn.fql.basic.util.LoginContext;
import cn.fql.org.domain.Employee;
import cn.fql.org.mapper.EmployeeMapper;
import cn.fql.pet.domain.Pet;
import cn.fql.pet.domain.PetDetail;
import cn.fql.pet.domain.PetOnlineAuditLog;
import cn.fql.pet.domain.PetType;
import cn.fql.pet.mapper.PetDetailMapper;
import cn.fql.pet.mapper.PetMapper;
import cn.fql.pet.mapper.PetOnlineAuditLogMapper;
import cn.fql.pet.mapper.PetTypeMapper;
import cn.fql.pet.service.IPetService;
import cn.fql.basic.service.impl.BaseServiceImpl;
import cn.fql.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Pet pet) {
        super.save(pet);
        if(pet.getPetDetail()!=null){
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void del(Long id) {
        petDetailMapper.delDetailByPetId(id);
        super.del(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetailMapper.updateDetailByPetId(pet);
        }
    }

    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            //1.审核 - 智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            if(boo1 && boo2){
                //审核通过 将状态改为1 就是上架 还有上架的时间 --最后添加审核日志
                petMapper.updateOnSaleState(id,new Date());
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            }else {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(Long[] ids) {
        //修改状态  修改下架的时间
        petMapper.updateState(ids,new Date());
    }


}
